package jwj8508.j2ee.basic.projectv5.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import jwj8508.j2ee.basic.projectv5.model.MemberVO;
import jwj8508.j2ee.basic.util.jdbc.MariaDB;

public class MemberDAO {
	private String newSQL = "insert into memberv4(uid, pwd, name) values(?, ?, ?)";
	private String loginSQL = "select * from memberv4 where uid=? and pwd=?";
	private String readSQL = "select * from memberv4 where uid=?";
	
	// 회원가입newMember
	public boolean newMember(MemberVO m) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		boolean result = false;
		try {
			conn = MariaDB.openConn();
			pstmt = conn.prepareStatement(newSQL);
			pstmt.setString(1, m.getUid());
			pstmt.setString(2, m.getPwd());
			pstmt.setString(3, m.getName());			
			if (pstmt.executeUpdate()>0)
				result = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MariaDB.closeConn(conn, pstmt, null);
		}
		return result;		
	}

	// 로그인login
	public boolean login(MemberVO m) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		boolean result = false;
		try {
			conn = MariaDB.openConn();
			pstmt = conn.prepareStatement(loginSQL);
			pstmt.setString(1, m.getUid());
			pstmt.setString(2, m.getPwd());
			rs = pstmt.executeQuery();
			if (rs.next()){ result = true; }
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MariaDB.closeConn(conn, pstmt, rs);
		}
		return result;
	}

	// 회원정보출력readMember
	public MemberVO readMember(MemberVO m) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		MemberVO result = null;
		try {
			conn = MariaDB.openConn();
			pstmt = conn.prepareStatement(readSQL);
			pstmt.setString(1, m.getUid());
			rs = pstmt.executeQuery();			
			if (rs.next()){
				result = new MemberVO();
				result.setMno(rs.getString("mno"));
				result.setUid(m.getUid());
				result.setPwd(rs.getString("pwd"));
				result.setName(rs.getString("name"));
				result.setRdate(rs.getString("rdate"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MariaDB.closeConn(conn, pstmt, rs);
		}
		return result;
	}

}
